package com.jz.digital.zhixiangruku

import com.jz.digital.base.BasePresenter
import com.jz.digital.bean.BaseEntity
import com.jz.digital.bean.ServerBean
import com.jz.digital.listener.ApiCallback
import com.jz.digital.net.DataApi
import com.jz.digital.utils.LogUtils
import java.util.concurrent.CountDownLatch

/**
 * @author jiangdz
 * @date 2024/11/19
 * @time 19:26
 * 用途:纸箱入库
 */
class ZhiXiangRuKuCommitPresenter:BasePresenter<IZhiXiangRuKuCommitView>() {
    private val tag="MaterialRequisitionCommitPresenter"
    val mCanBieList = mutableListOf<ServerBean.GXInfo>()
    val mTeamGroupList = mutableListOf<ServerBean.GXInfo>()
    val mTeamClassList = mutableListOf<ServerBean.GXInfo>()
    private val mCountDown= CountDownLatch(3)

    fun initData() {
        getTeamGroupFromServer()
        getTeamClassFromServer()
        getCanBieFromServer()
    }

    private fun getTeamGroupFromServer() {
        LogUtils.d(tag,"生产领料-开始从服务器获取班组列表")
        DataApi.api.getGXInfo(ServerBean.GXInfoRequest(mode = 1, type = "JT"))
            .enqueue(object : ApiCallback<BaseEntity<List<ServerBean.GXInfo>?>>() {
                override fun onSuccess(response: BaseEntity<List<ServerBean.GXInfo>?>?) {
                    LogUtils.d(tag, "生产领料-工序列表获取成功 response=$response")
                    if (response != null && !response.data.isNullOrEmpty()) {
                        mTeamClassList.addAll(response.data!!)
                    }
                    typeLoadFinish()
                }

                override fun onError(t: Throwable?, code: Int, message: String?) {
                    LogUtils.d(
                        tag,
                        "生产领料-工序列表获取失败 error=${t?.message},code=$code,message=$message"
                    )
                    typeLoadFinish()
                }

            })
    }

    private fun getTeamClassFromServer() {
        LogUtils.d(tag,"生产领料-开始从服务器获取班组列表")
        DataApi.api.getGXInfo(ServerBean.GXInfoRequest(mode = 1, type = "JT"))
            .enqueue(object : ApiCallback<BaseEntity<List<ServerBean.GXInfo>?>>() {
                override fun onSuccess(response: BaseEntity<List<ServerBean.GXInfo>?>?) {
                    LogUtils.d(tag, "生产领料-工序列表获取成功 response=$response")
                    if (response != null && !response.data.isNullOrEmpty()) {
                        mTeamClassList.addAll(response.data!!)
                    }
                    typeLoadFinish()
                }

                override fun onError(t: Throwable?, code: Int, message: String?) {
                    LogUtils.d(
                        tag,
                        "生产领料-工序列表获取失败 error=${t?.message},code=$code,message=$message"
                    )
                    typeLoadFinish()
                }

            })
    }

    private fun getCanBieFromServer() {
        LogUtils.d(tag,"生产领料-开始从服务器获取班组列表")
        DataApi.api.getGXInfo(ServerBean.GXInfoRequest(mode = 1, type = "JT"))
            .enqueue(object : ApiCallback<BaseEntity<List<ServerBean.GXInfo>?>>() {
                override fun onSuccess(response: BaseEntity<List<ServerBean.GXInfo>?>?) {
                    LogUtils.d(tag, "生产领料-工序列表获取成功 response=$response")
                    if (response != null && !response.data.isNullOrEmpty()) {
                        mCanBieList.addAll(response.data!!)
                    }
                    typeLoadFinish()
                }

                override fun onError(t: Throwable?, code: Int, message: String?) {
                    LogUtils.d(
                        tag,
                        "生产领料-工序列表获取失败 error=${t?.message},code=$code,message=$message"
                    )
                    typeLoadFinish()
                }

            })
    }

    private fun typeLoadFinish(){
        mCountDown.countDown()
        if (mCountDown.count==0L){
            view?.dismissLoading()
        }
    }
}